package com.task.scheduling.service.impl;

import com.task.scheduling.dao.TaskSchedulingDao;
import com.task.scheduling.job.CheckDeadJob;
import com.task.scheduling.job.ReviveJob;
import com.task.scheduling.job.RunNewJob;
import com.task.scheduling.service.ApplicationReadyAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @program: task-scheduling
 * @description: 初始化系统，启动线程
 * @author: zhouliangliang
 * @create: 2018-11-14 17:03
 **/
@Service
public class InitSystem implements ApplicationReadyAware<Void> {

    private Executor executor = Executors.newFixedThreadPool(3);

    @Autowired
    private TaskSchedulingDao taskSchedulingDao;

    public InitSystem(TaskSchedulingDao taskSchedulingDao) {
        this.taskSchedulingDao = taskSchedulingDao;
    }

    @Override
    public <Void> Void afterApplicationReady(ApplicationContext context) {
        System.out.println(taskSchedulingDao);
        executor.execute(new RunNewJob(taskSchedulingDao));
        executor.execute(new ReviveJob(taskSchedulingDao));
        executor.execute(new CheckDeadJob(taskSchedulingDao));
        return null;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
